<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>JSON</title>
	</head>

	<body>
		<h1>json</h1>
		<script>
			//JSON(JS 对象简谱)是js的一个子集
			//JSON.parse(String)  将字符串内容转成对应的类型
			//JSON.stringify(Object)   将一个对象转成字符串

			var n1 = 1;
			n1 = "1";
			n1 = JSON.parse(n1);
			console.info(n1 + " " + typeof(n1));
			var b1 = true;
			b1 = "false";
			b1 = JSON.parse(b1);
			console.info(b1 + " " + typeof(b1));

			//JSON中的字符串表达方式只接受双引号
			var s1 = "ab\"c";
			s1 = '"ab\\"c"';
			s1 = JSON.parse(s1);
			//s1 = JSON.parse('"ab\\"c"');
			console.info(s1);
			var s2 = 'ab\'c';
			s2 = "'ab\\'c'";
			//s2 = JSON.parse(s2);//不支持
			console.info(s2);

			//JSON中对于特殊字符支持null，不支持undefined
			var nu1 = null;
			nu1 = "null";
			nu1 = JSON.parse(nu1);
			console.info(nu1 + " " + typeof(nu1));
			var un1 = undefined;
			un1 = "undefined";
			//un1 = JSON.parse(un1);//不支持
			console.info(un1 + " " + typeof(un1));

			var obj1 = new Object();
			obj1.a = 1;
			obj1["b"] = "aa";
			console.info(obj1);
			console.info(JSON.stringify(obj1));
			console.info(JSON.stringify(obj1, null, "\t"))

			//JSON中对于引用类型支持对象和数组；对象的表示法：对象的属性必须加上双引号
			var obj2 = {
				a: 2,
				b: "bb"
			};
			obj2 = `{a:2,b:"bb"}`;
			//obj2 = JSON.parse(obj2);//不支持
			console.info(obj2);

			var obj3 = {
				"a": 3,
				"b": "cc"
			};
			obj3 = `{"a":3,"b":"cc"}`;
			obj3 = JSON.parse(obj3);
			console.info(obj3);

			var arr1 = new Array();
			arr1[0] = "aa";
			arr1[10] = true;
			console.info(arr1);
			console.info(JSON.stringify(arr1));

			var arr2 = ["bb", null, false];
			arr2 = `["bb",null,false]`;
			arr2 = JSON.parse(arr2);
			console.info(arr2);

			var classInfo = {
				"name": "java1班",
				"num": 1,
				"isJava": true,
				"list": [{
					"name": "张三",
					"age": 18,
					"sex": true
				}, {
					"name": "李四",
					"age": 20,
					"sex": false
				}]
			};
			classInfo = `{
				"name":"java1班",
				"num":1,
				"isJava":true,
				"list":[
					{
						"name":"张三",
						"age":18,
						"sex":true
					},{
						"name":"李四",
						"age":20,
						"sex":false
					}
				]
			}`;
			console.info(classInfo);
			classInfo = JSON.parse(classInfo);
			console.info(classInfo);
			console.info(classInfo.list[0].name);
			console.info(JSON.stringify(classInfo));
			console.info(JSON.stringify(classInfo,null,"\t"));
		</script>
	</body>

</html>